package DataBase;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Vector;

import DataObjects.FireForManageTableOrder;
import DataObjects.RecipeAdditions;

public class GetFiresByOrderId {

	
	private String orderId;
	private int orderIdInt;
	
	public GetFiresByOrderId(String orderID)
	{
		this.orderId=orderID;
		this.orderIdInt=Integer.parseInt(orderID);
	}
	
	public Vector<FireForManageTableOrder> getFires()
	{
		Vector<FireForManageTableOrder> vectorFires=new Vector<FireForManageTableOrder>();
		
		SQLConnection sc = new SQLConnection();

		try {

			PreparedStatement ps = sc.getConnection().prepareStatement("SELECT orderId,rid,rname,isFired,isSubmitted,timeFire,timeSubmit,mid,extraName,extraRid,souceName,souceRid,comments,actionLevel,quantity  FROM Fire WHERE orderId = ?;");
			ps.setInt(1, this.orderIdInt);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) 
			{
				FireForManageTableOrder fire=new FireForManageTableOrder();
				fire.setOrderId(rs.getInt(1));
				fire.setRid(rs.getInt(2));
				fire.setNameOfRecipe(rs.getString(3));
				if(rs.getInt(4)==0) fire.setFired(false);
				if(rs.getInt(4)==1) fire.setFired(true);
				if(rs.getInt(5)==0) fire.setSubmitted(false);
				if(rs.getInt(5)==1) fire.setSubmitted(true);
				DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
				if(rs.getTime(6)!=null)
					fire.setTimeOfFire(dateFormat.format(rs.getTime(6)));
				else
					fire.setTimeOfFire(null);
				
				if(rs.getTime(7)!=null)
					fire.setTimeOfSubmitted(dateFormat.format(rs.getTime(7)));
				else
					 fire.setTimeOfSubmitted(null);
				
                fire.setMid(rs.getInt(8));
                RecipeAdditions extra=new RecipeAdditions();
                RecipeAdditions souce = new RecipeAdditions();
                extra.setName(rs.getString(9));
                extra.setId(Integer.toString(rs.getInt(10)));
                souce.setName(rs.getString(11));
                souce.setId(Integer.toString(rs.getInt(12)));
                fire.setExtra(extra);
                fire.setSouce(souce);
                fire.setComments(rs.getString(13));
                fire.setActionLevel(rs.getString(14));
                fire.setQuantity(rs.getInt(15));
                vectorFires.add(fire);               
			}// while
		     ps.close();
			 rs.close();
			 sc.getConnection().close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
		
		return vectorFires;
		
	}
}
